<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片式图 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click='showAddCateDialog'>添加分类</el-button>
        </el-col>
      </el-row>

      <!-- 表格 -->
      <tree-table style="margin-top: 20px;" :data='catelist' :columns='columns' :selection-type='false'
        :expand-type='false' show-index index-text='#' border :show-row-hover='false'>
        <!-- 是否有效 -->
        <template slot='isok' slot-scope='scope'>
          <i class="el-icon-success" v-if='scope.row.cat_deleted === false'></i>
          <i class="el-icon-error" v-else></i>
        </template>
        <!-- 排序 -->
        <template slot='order' slot-scope='scope'>
          <el-tag v-if='scope.row.cat_level === 0'>一级</el-tag>
          <el-tag type='success' v-else-if='scope.row.cat_level === 1'>二级</el-tag>
          <el-tag type="warning" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot='cao' slot-scope='scope'>
          {{scope.ro}}
          <el-button type="primary" icon="el-icon-edit">编辑</el-button>
          <el-button type="danger " icon="el-icon-delete">删除</el-button>
        </template>
      </tree-table>

      <!-- 分页区 -->
      <div class="block">
        <el-pagination layout="total,prev, pager, next,jumper" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" :current-page="querInfo.pagenum" :page-size='querInfo.pagesize'
          :total="30">
        </el-pagination>
      </div>
    </el-card>

    <!-- 添加分类的对话框 -->
    <el-dialog title="提示" :visible.sync="addDiaLogVisBle" width="50%" @close='addCateDialogVisible'>
      <el-form :model="addCateForm" :rules="addCateFormRules" ref="addCateFormRef" label-width="100px">
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <div class="block">
        <span class="demonstration" style="margin-left: 30px; ">父级分类：</span>
        <el-cascader style='width: 472px;' clearable v-model="selectKeys" :options="parentCateList"
          :props="cascaderProps" @change='parentCateChange' change-on-select>
        </el-cascader>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDiaLogVisBle = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script scoped>
  export default {
    data() {
      return {
        // 选中的父级分类的id数组
        selectKeys: [],
        querInfo: {
          // 每页显示多少数据
          pagesize: 5,
          // 页码值
          pagenum: 1
        },
        // 商品分类的数据列表，默认为空
        catelist: [],
        // 总条数
        total: 0,
        // 添加分类的表单数据对象
        addCateForm: {},
        // 将要添加的分类名称
        cat_name: '',
        // 父级分类的id
        cat_pid: 0,
        // 分类等级
        cat_level: 0,
        // 父级分类列表
        parentCateList: [],
        // 指定级联配置对象
        cascaderProps: {
          expandTrigger: 'hover',
          value: 'cat_id',
          label: 'cat_name',
          children: 'children'
        },
        // 添加分类表单的验证规则
        addCateFormRules: {
          cat_name: [{
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          }]
        },
        // 控制添加对话框是否显示
        addDiaLogVisBle: false,
        // 为table指定列的定义
        columns: [{
          label: '分类名称',
          prop: 'cat_name'
        }, {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        }, {
          label: '排序',
          type: 'template',
          template: 'order'
        }, {
          label: '操作',
          type: 'template',
          template: 'cao'
        }]
      }
    },
    methods: {
      addCateDialogVisible() {
        this.$refs.addCateFormRef.resetFields()
        this.selectKeys = []
        this.addCateForm.cat_level = 0
        this.addCateForm.cat_pid = 0
      },
      // 点击按钮
      addCate() {
        this.$refs.addCateFormRef.validate(async valid => {
          if (!valid) return
          const {
            data: res
          } = await this.$axios({
            url: 'categories',
            method: 'POST',
            data: this.addCateForm
          })
          if (res.meta.status !== 201) return this.$message.error('添加分类失败!')
          this.$message.success('添加分类成功!')
          this.getCate()
          this.addDiaLogVisBle = false
        })
      },
      // 选择项发生变化出发这个函数
      parentCateChange() {
        if (this.selectKeys.length > 0) {
          this.addCateForm.cat_pid = this.selectKeys[this.selectKeys.length - 1]
          this.addCateForm.cat_level = this.selectKeys.length
        } else {
          this.addCateForm.cat_pid = 0
          this.addCateForm.cat_level = 0
        }
      },
      async getParentCateList() {
        // 获取父级分类数据列表
        const {
          data: res
        } = await this.$axios({
          method: 'GET',
          url: 'categories',
          params: {
            type: 2
          }
        })
        if (res.meta.status !== 200) return this.$message.error('获取父级分类失败')
        this.parentCateList = res.data
      },
      // 点击按钮展示添加分类对话框
      showAddCateDialog() {
        this.getParentCateList()
        this.addDiaLogVisBle = true
      },
      handleSizeChange(newSize) {
        this.querInfo.pagesize = newSize
        this.getCate()
      },
      handleCurrentChange(newPage) {
        this.querInfo.pagenum = newPage
        this.getCate()
      },
      // 获取商品分类数据
      async getCate() {
        const {
          data: res
        } = await this.$axios.get('categories', {
          params: this.querInfo
        })
        if (res.meta.status !== 200) return this.$message.error('获取数据失败!')
        this.catelist = res.data.result
        // 为总数据条数赋值
        this.total = res.data.total
      }
    },
    created() {
      this.getCate()
    }
  }

</script>

<style scoped>
  .el-icon-success {
    color: lightgreen;
  }

  .el-icon-error {
    color: red;
  }

</style>
